package com.blue.controller.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.job.ScheduledJobDTO;
import com.blue.po.job.ScheduledJobPO;
import com.blue.service.job.IScheduledJobService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 定时任务表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2023-10-03
 */
@RestController
@RequestMapping("/job/scheduledJob")
@Tag(name = "系统任务调度")
public class ScheduledJobController {
    @Autowired
    private IScheduledJobService scheduledJobService;

    /**
     * 列表查询
     */
    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("className,remark")
    public ResponseResult<?> list(ScheduledJobDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        ScheduledJobPO scheduledJobPO = ConvertUtils.convert(ScheduledJobPO.class, requestDTO);
        MPJQueryWrapper<ScheduledJobPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(scheduledJobPO);
        Page<ScheduledJobPO> page = new Page<>(pageNum, pageSize);
        Page<ScheduledJobPO> pageResult = scheduledJobService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody ScheduledJobDTO requestDTO) {
        List<ScheduledJobPO> scheduledJobPOList = scheduledJobService.list(new LambdaQueryWrapper<ScheduledJobPO>()
                .eq(ScheduledJobPO::getClassName, requestDTO.getClassName()));
        if (!CollectionUtils.isEmpty(scheduledJobPOList)) {
            return ResponseResult.errorResult("全类名已存在！");
        }
        ScheduledJobPO scheduledJobPO = ConvertUtils.convert(ScheduledJobPO.class, requestDTO);
        scheduledJobService.add(scheduledJobPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody ScheduledJobDTO requestDTO) {
        ScheduledJobPO scheduledJobPO = ConvertUtils.convert(ScheduledJobPO.class, requestDTO);
        scheduledJobService.edit(scheduledJobPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> edit(@RequestBody List<String> ids) {
        scheduledJobService.delete(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody ScheduledJobDTO requestDTO) {
        ScheduledJobPO scheduledJobPO = scheduledJobService.getById(requestDTO.getId());
        return ResponseResult.okResult(scheduledJobPO);
    }

    @PostMapping("/execute")
    @Operation(summary = "执行任务调度")
    public ResponseResult<?> execute(@RequestBody ScheduledJobDTO requestDTO) {
        ScheduledJobPO scheduledJobPO = ConvertUtils.convert(ScheduledJobPO.class, requestDTO);
        scheduledJobService.execute(scheduledJobPO);
        return ResponseResult.okResult();
    }
}
